/*
 * @Date: 2024-03-10 21:42:10
 * @LastEditors: 玄绮 shijiahansi@qq.com
 * @LastEditTime: 2024-04-13 13:35:10
 * @FilePath: \server\src\server\app\ChatRoom.cpp
 */
#include "ChatRoom.h"

#include "ChatSession.h"
#include "ChatSessionManager.h"
#include "data/UserInfo.pb.h"

#include <ChatServer.h>
#include <algorithm>
#include <boost/bind/placeholders.hpp>
#include <signal.hpp>


ChatRoom::ChatRoom(data::ChatRoomInfo info) {
    m_info = info;
    getRoomsMembers(info.roomid(), m_users);
}

ChatRoom::~ChatRoom() {
    // connectionUserLogin.disconnect();
    // std::clog << "    connectionUserLogin.disconnect();" << std::endl;
    // connectionUserUnLogin.disconnect();
    // std::clog << "    connectionUserUnLogin.disconnect();" << std::endl;
}

void ChatRoom::deliver(data::Message msg) {
    try {
        // 发送 信号，有新消息来了
        signalRoomNewMessage(msg);
    }
    catch (const std::exception& e) {
        std::cerr << e.what() << '\n';
    }

    m_messages.push_back(msg);
}

const ChatRoom::MessageVector& ChatRoom::getMessages() const {
    // TODO: 在此处插入 return 语句
    return m_messages;
}

const std::vector<data::UserInfo>& ChatRoom::getMembers() const {
    return m_users;
}

data::ChatRoomInfo ChatRoom::getInfomation() const {
    return m_info;
}

void ChatRoom::setInfomation(data::ChatRoomInfo info) {
    m_info = info;
}

void ChatRoom::join(std::string userId) {
    // auto& sessons = Application::getInstance().sessionManager();
    // auto m        = sessons.getSession(m_info.userid());

    // if (m){

    // }else{

    // }

    data::UserInfo user;
    getUserInfo(userId, user);
    addRoomMember(m_info.roomid(), userId);
    m_users.push_back(user);
}

void ChatRoom::join(std::string userId, ChatSession::SharedPtr session) {
    join(userId);
    signald::connect(signalRoomNewMessage, &ChatSession::slotOnRoomNewMessage, session);
}

void ChatRoom::leave(std::string userId) {
    auto p = std::find_if(m_users.begin(), m_users.end(), [userId](const proto::data::UserInfo& item) {
        return item.userid() == userId;
    });
    if (p != m_users.end()) {
        m_users.erase(p);
        delRoomMember(m_info.roomid(), userId);
    }
}

bool ChatRoom::contains(std::string userId) {
    // 查找元素
    auto it = std::find_if(m_users.begin(), m_users.end(), [userId](const proto::data::UserInfo& item) {
        return item.userid() == userId;
    });
    return (it != m_users.end());
}

std::string ChatRoom::roomid() {
    return m_info.roomid();
}

const data::ChatRoomInfo& ChatRoom::info() {
    // TODO: 在此处插入 return 语句
    return m_info;
}

void ChatRoom::slotOnUserLogin(std::string userId, ChatSession::SharedPtr session) {
    if (contains(userId)) {
        // 使用 slot_type 自动管理槽函数的连接
        // 当 session 被销毁时 将自动断开与他的槽函数的连接
        signald::connect(signalRoomNewMessage, &ChatSession::slotOnRoomNewMessage, session);
    }
}

void ChatRoom::slotOnUserUnLogin(std::string userId, ChatSession::SharedPtr session) {
    std::clog << m_info.roomid() << ": slotOnUserUnLogin:" << userId << std::endl;
}

void ChatRoom::initConnect() {
    auto& manager = ChatServer::getInstance().sessionManager();

    signald::connect(manager.signalUserLogin, &ChatRoom::slotOnUserLogin, shared_from_this());
    // signald::connect(manager.signalUserUnLogin, &ChatRoom::slotOnUserUnLogin, shared_from_this());
}
